package com.example.myapplication.part03.part08;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.os.Bundle;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;


public class part8_02Activity extends AppCompatActivity {

    private ProgressBar circularProgressBar;
    private ProgressBar progressBar;
    private TextView progressText;
    private Button startButton, pauseButton, resetButton;
    private ValueAnimator progressAnimator;
    private boolean isPaused = false;
    private int lastProgress = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_part802);

        // 初始化视图
        circularProgressBar = findViewById(R.id.circularProgressBar);
        progressBar = findViewById(R.id.progressBar);
        progressText = findViewById(R.id.progressText);
        startButton = findViewById(R.id.startButton);
        pauseButton = findViewById(R.id.pauseButton);
        resetButton = findViewById(R.id.resetButton);

        // 默认隐藏圆形进度条
        circularProgressBar.setVisibility(View.GONE);
        resetButton.setEnabled(false);

        // 设置按钮点击监听
        startButton.setOnClickListener(v -> startProgressAnimation());
        pauseButton.setOnClickListener(v -> togglePause());
        resetButton.setOnClickListener(v -> resetProgress());
    }

    private void startProgressAnimation() {
        // 重置状态确保从0开始
        lastProgress = 0;

        // 显示圆形进度条
        circularProgressBar.setVisibility(View.VISIBLE);

        // 更新按钮状态
        startButton.setEnabled(false);
        pauseButton.setEnabled(true);
        resetButton.setEnabled(false);

        // 设置暂停状态
        isPaused = false;
        pauseButton.setText("暂停");

        // 取消之前的动画（如果有）
        if (progressAnimator != null) {
            progressAnimator.cancel();
        }

        // 创建值动画器
        progressAnimator = ValueAnimator.ofInt(0, 100);
        progressAnimator.setDuration(3000); // 3秒完成
        progressAnimator.setInterpolator(new DecelerateInterpolator());

        // 更新进度
        progressAnimator.addUpdateListener(animation -> {
            if (!isPaused) {
                int progress = (int) animation.getAnimatedValue();
                progressBar.setProgress(progress);
                progressText.setText(progress + "%");
                updateProgressColor(progress);
                lastProgress = progress;
            }
        });

        // 动画结束回调
        progressAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                circularProgressBar.setVisibility(View.GONE);
                startButton.setEnabled(true);
                pauseButton.setEnabled(false);
                resetButton.setEnabled(true);

                if (!isPaused) {
                    Toast.makeText(part8_02Activity.this, "加载完成!", Toast.LENGTH_SHORT).show();
                }
            }
        });

        progressAnimator.start();
    }

    private void togglePause() {
        isPaused = !isPaused;

        if (isPaused) {
            pauseButton.setText("继续");
            circularProgressBar.setVisibility(View.GONE);
            if (progressAnimator != null) {
                progressAnimator.pause();
            }
            resetButton.setEnabled(true); // 暂停时允许重置
        } else {
            pauseButton.setText("暂停");
            circularProgressBar.setVisibility(View.VISIBLE);
            if (progressAnimator != null) {
                progressAnimator.resume();
            }
            resetButton.setEnabled(false); // 继续时禁用重置
        }
    }

    private void updateProgressColor(int progress) {
        int color;
        if (progress < 30) {
            color = Color.RED;
        } else if (progress < 70) {
            color = Color.YELLOW;
        } else {
            color = Color.GREEN;
        }
        progressBar.getProgressDrawable().setColorFilter(
                new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
    }

    private void resetProgress() {
        // 1. 取消当前动画
        if (progressAnimator != null) {
            progressAnimator.cancel();
            progressAnimator.removeAllListeners();
            progressAnimator.removeAllUpdateListeners();
        }

        // 2. 重置所有进度相关变量
        lastProgress = 0;
        isPaused = false;

        // 3. 重置UI状态
        progressBar.setProgress(0);
        progressText.setText("0%");
        updateProgressColor(0);
        circularProgressBar.setVisibility(View.GONE);

        // 4. 重置按钮状态
        startButton.setEnabled(true);
        pauseButton.setEnabled(false);
        pauseButton.setText("暂停");
        resetButton.setEnabled(false);

        // 5. 清理动画对象
        progressAnimator = null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progressAnimator != null) {
            progressAnimator.cancel();
        }
    }
}